package generic;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import generic.service.GenericService;
import generic.types.DataObject;
import generic.types.Property;

import java.text.SimpleDateFormat;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * Generic MeterReadings service
 * @author Markus Jung
 *
 */
@WebService(name="GenericService", targetNamespace="http://genericservice")
public class GenericServiceImpl implements GenericService {
	private Map<String, DataObject> dataObjects = new HashMap<String, DataObject>();
	
	public GenericServiceImpl(){
		createDataObjects();
	}
	
	@WebMethod
	public DataObject readData(@WebParam(name="id") String id) {
		return dataObjects.get("/meter/12345");
	}
	
	@WebMethod
	public void writeData(@WebParam(name="id") String id, @WebParam(name="data") DataObject data) {
		
	}
	
	private void createDataObjects(){
		DataObject meterReadings = new DataObject();
		Property id = new Property("id", "/meter/12345");
		meterReadings.getProperties().add(id);
		Property meterId = new Property("meterID", "12345");
		Property meterType = new Property("meterType", "PRIVATE_HOUSEHOLD");
		meterReadings.getProperties().add(meterId);
		meterReadings.getProperties().add(meterType);
				
		for(int i=0; i< 10; i++){
			DataObject reading = new DataObject();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
			Property date = new Property("date", sdf.format(new Date(System.currentTimeMillis() - (System.currentTimeMillis() % (1000 * 60 * 15)) + (1000 * 60 * 15 * i))));
			Property type = new Property("type", "datetime");
			date.getProperties().add(type);
			reading.getProperties().add(date);			
			Property power = new Property("power", "" + (Math.random() * 800 + 200));
			reading.getProperties().add(power);
			type = new Property("type", "number");
			power.getProperties().add(type);
			Property unit = new Property("unit", "WATT");
			power.getProperties().add(unit);
			Property energy = new Property("energy", "" + (i + Math.random() * 1));
			reading.getProperties().add(energy);
			type = new Property("type", "number");			
			unit = new Property("unit", "KILOWATTHOURS");
			energy.getProperties().add(type);
			energy.getProperties().add(unit);			
			Property interval = new Property("interval", "FIFTEEN_MINUTES");
			reading.getProperties().add(interval);
			meterReadings.getDataObjects().add(reading);		
		}	
		dataObjects.put("/meter/12345", meterReadings);								
	}	
}
